package com.example.elmspringboot.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.elmspringboot.controller.utils.CartDto;
import com.example.elmspringboot.controller.utils.OrderDetailDto;
import com.example.elmspringboot.domain.Cart;
import com.example.elmspringboot.domain.Food;
import com.example.elmspringboot.domain.OrderDetail;
import com.example.elmspringboot.mapper.OrderDetailMapper;
import com.example.elmspringboot.service.OrderDetailService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OrderDetailImpl extends ServiceImpl<OrderDetailMapper, OrderDetail> implements OrderDetailService {
    @Resource
    private OrderDetailMapper orderDetailMapper;
    @Override
    public void addOrderDet(OrderDetail orderDetail) {
        QueryWrapper<OrderDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", orderDetail.getOrderId()).eq("food_id", orderDetail.getFoodId());
        OrderDetail orderDetailEntity = orderDetailMapper.selectOne(wrapper);
        if(orderDetailEntity != null){
            orderDetailEntity.setQuantity(orderDetail.getQuantity());
            orderDetailMapper.updateById(orderDetailEntity);
        }
        else {
            orderDetailMapper.insert(orderDetail);
        }
    }

    @Override
    public List<OrderDetailDto> getOrderDetailList(Integer orderId) {
        MPJLambdaWrapper<OrderDetail> wrapper = new MPJLambdaWrapper<OrderDetail>()
                .selectAll(OrderDetail.class)
                .select(Food::getPrice).select(Food::getDescription).select(Food::getName).select(Food::getImgUrl)
                .innerJoin(Food.class, Food::getId, OrderDetail::getFoodId).eq(OrderDetail::getOrderId, orderId);
        return orderDetailMapper.selectJoinList(OrderDetailDto.class, wrapper);
    }
}
